package hbase.app

import java.text.SimpleDateFormat

import org.apache.commons.lang3.time.FastDateFormat
import org.apache.spark.sql.SparkSession

/**
  * Created on 2019-08-15 15:47.
  */
object SparkUdf {
  def main(args: Array[String]): Unit = {
    val spark = SparkSession.builder().master("local[*]").getOrCreate()

    import spark.implicits._

    //生成一个DataFrame
    val df = Seq(
      (1, "boy", "裤子"),
      (2, "girl", "裤子"),
      (3, "boy", "裙子"),
      (4, "girl", "裙子"),
      (5, "girl", "裙子")
    ).toDF("id", "sex", "dressing")
    df.createOrReplaceTempView("boys_and_girls")

    //找变态函数
    def findHentai(sex:String,dressing:String): String ={
      if(sex =="boy" && dressing == "裙子") "变态" else "正常"
    }


    spark.udf.register("find_hentai",findHentai _ )

    spark.sql(s"select id,sex,dressing,find_hentai(sex,dressing) as tag from boys_and_girls").show()

  }
}
